package com.atguigu.yygh.apartment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.apartment.repository.ChamberMongoDbRepository;
import com.atguigu.yygh.apartment.repository.ChamberRepository;
import com.atguigu.yygh.apartment.service.ChamberService;
import com.atguigu.yygh.model.hosp.Chamber;
import com.atguigu.yygh.model.hosp.ChamberMongodb;
import com.atguigu.yygh.vo.hosp.ChamberQueryVo;

import com.atguigu.yygh.vo.hosp.ChamberVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import com.atguigu.yygh.apartment.config.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ChamberServiceImpl implements ChamberService {
    @Autowired
    private ChamberRepository chamberRepository;


    //上传寝室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //paramMap转换成Chamber对象
        String jsonString = JSONObject.toJSONString(paramMap);
        //Chamber chamber= JSONObject.parseObject(jsonString,Chamber.class);
        Chamber chamber = JSONObject.parseObject(jsonString,Chamber.class);
        //根据 宿舍 和 寝室 编号查询
        Chamber chamberExist = chamberRepository.getChamberByApartmentCodeAndChamberCode(chamber.getApartmentCode(),chamber.getChamberCode());

        if (chamberExist!=null) {
            chamberExist.setUpdateTime(new Date());
            chamberExist.setIsDeleted(0);
            chamberExist.setBigname(chamber.getBigname());
            chamberExist.setIntro(chamber.getIntro());
            chamberExist.setChamberName(chamber.getChamberName());
            chamberRepository.save(chamberExist);
        }else {
            chamber.setCreateTime(new Date());
            chamber.setUpdateTime(new Date());
            chamber.setIsDeleted(0);

            chamberRepository.save(chamber);
        }
    }

    //查询寝室接口
    @Override
    public Page<Chamber> findPageChamber(int page, int limit, ChamberQueryVo chamberQueryVo) {
        //创建pageable 设置当前页and记录数
        //0 为第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建Ex对象,传入对象

        //Chamber对象
        Chamber chamber=new Chamber();
        BeanUtils.copyProperties(chamberQueryVo,chamber);
        chamber.setIsDeleted(0);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//模糊查找
                .withIgnoreCase(true);//忽略大小写
        Example<Chamber> example = Example.of(chamber,matcher);
        Page<Chamber> all = chamberRepository.findAll(example, pageable);
        //System.out.println("chamberAll:"+all.getContent().toString());
        return all;
    }

    //删除寝室接口
    @Override
    public void remove(String apartmentCode, String chamberCode) {
        //判断是否存在
        Chamber chamber = chamberRepository.getChamberByApartmentCodeAndChamberCode(apartmentCode, chamberCode);
        if (chamber!=null) {
            //调用方法
            chamberRepository.deleteById(chamber.getId());
        }
    }

    @Override
    public List<ChamberVo> findChamberTree(String apartmentCode) {
        //封装最终数据
        List<ChamberVo> result = new ArrayList<>();
        //根据宿舍code,查询寝室
        Chamber chamberQuery = new Chamber();
        chamberQuery.setApartmentCode(apartmentCode);
        Example<Chamber> example = Example.of(chamberQuery);

        List<Chamber> chamberList = chamberRepository.findAll(example);
        //根据楼层号分组，获得每层楼的宿舍
        Map<String, List<Chamber>> chamberMap = chamberList.stream().collect(Collectors.groupingBy(Chamber::getBigname));
        //遍历map
        for (Map.Entry<String,List<Chamber>> entry: chamberMap.entrySet()){
            //楼层编号
            String bigCode = entry.getKey();
            //楼层所有房间
            List<Chamber> chamberList1 = entry.getValue();

            //封装所有楼层
            ChamberVo chamberVo=new ChamberVo();
            chamberVo.setChamberCode(bigCode);
            chamberVo.setChamberName(chamberList1.get(0).getBigname());
            //封装楼层所有房间
            List<ChamberVo> children = new ArrayList<>();
            for (Chamber chamber : chamberList1) {
                ChamberVo chamberVo2 = new ChamberVo();
                chamberVo2.setChamberCode(chamber.getChamberCode());
                chamberVo2.setChamberName(chamber.getChamberName());
                //封装到list中
                children.add(chamberVo2);
            }
            chamberVo.setChildren(children);
            //放入最终result
            result.add(chamberVo);
        }
        return result;
    }

    @Override
    public String getChamberName(String apartmentCode, String chamberCode) {
        Chamber chamber = chamberRepository.getChamberByApartmentCodeAndChamberCode(apartmentCode, chamberCode);
        if (chamber!=null) {
            return chamber.getChamberName();
        }
        return null;
    }
}
